﻿using Navigant.Infrastructure.RepositoryFramework;
using System.Collections.Generic;

namespace Navigant.Infrastructure.DomainBase
{
    public abstract class Service<T> where T : IAggregateRoot
    {
        private IUnitOfWork unitOfWork;
        private IRepository<T> repository;

        protected Service()
            : this(null)
        {
        }

        protected Service(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }

        protected virtual IRepository<T> GetRepository()
        {
            return RepositoryFactory.GetRepository<IRepository<T>, T>(this.unitOfWork);
        }

        protected IRepository<T> Repository
        {
            get 
            {
                if (this.repository == null)
                {
                    this.repository = this.GetRepository();
                }
                return this.repository; 
            }
        }

        protected IUnitOfWork UnitOfWork
        {
            get { return this.unitOfWork; }
        }

        public T FindBy(object key)
        {
            return this.Repository.FindBy(key);
        }

        public IList<T> FindAll()
        {
            return this.Repository.FindAll();
        }
    }
}
